//package cqrtplm.aop;
//
//import cn.hutool.core.collection.CollUtil;
//import com.hustcad.plm.pdm.downloadcenter.model.vo.DownloadOptionVO;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.stereotype.Component;
//import org.springframework.util.FileCopyUtils;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.util.ContentCachingResponseWrapper;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Enumeration;
//import java.util.List;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
//
//@Aspect
//@Component
//@Slf4j
//public class SDBatchDownloadFileByDocListAop {
//
//    @Around(value = "execution(* com.hustcad.plm.pdm.downloadcenter.service.TyplmFileDownCenterService.batchDownloadFileByDocList(..))")
//    public Object processZipResponse(ProceedingJoinPoint joinPoint) throws Throwable {
//        Object[] args = joinPoint.getArgs();
//        DownloadOptionVO downloadOptionVO = (DownloadOptionVO)args[0];
//        HttpServletResponse response = (HttpServletResponse) args[1];
//
//        ContentCachingResponseWrapper wrappedResponse = new ContentCachingResponseWrapper(response);
//        Object[] newArgs = Arrays.copyOf(args, args.length);
//        newArgs[1] = wrappedResponse;
//
//        Object result = joinPoint.proceed(newArgs);
//
//        byte[] zipData = wrappedResponse.getContentAsByteArray();
//        if (zipData == null || zipData.length == 0) {
//            wrappedResponse.copyBodyToResponse();
//            return result;
//        }
//
//        try {
//            List<MultipartFile> files = extractZipToMemory(zipData);
//
//            if (CollUtil.isNotEmpty(files) && files.size() == 1) {
//                MultipartFile file = files.get(0);
//                String fileName = file.getOriginalFilename();
//
//                response.reset();
//                response.setContentType("application/octet-stream");
//
//                String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())
//                        .replace("+", "%20");
//
//                response.setHeader("Content-Disposition",
//                                   "attachment; filename=\"" + encodedFileName + "\"; filename*=utf-8''" + encodedFileName);
//
//                try (InputStream in = file.getInputStream()) {
//                    FileCopyUtils.copy(in, response.getOutputStream());
//                }
//            } else {
//                response.reset();
//                response.setContentType("application/zip");
//                String encodedFileName = URLEncoder.encode(downloadOptionVO.getZipFileName(), StandardCharsets.UTF_8.name())
//                        .replace("+", "%20");
//                response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=utf-8''" + encodedFileName);
//                FileCopyUtils.copy(zipData, response.getOutputStream());
//            }
//        } catch (IOException e) {
//            throw new RuntimeException("ZIP处理失败", e);
//        }
//
//        return result;
//    }
//
//    private List<MultipartFile> extractZipToMemory(byte[] zipData) throws IOException {
//        List<MultipartFile> files = new ArrayList<>();
//        Path tempFile = null;
//
//        try {
//            tempFile = Files.createTempFile("temp_zip_", ".zip");
//            Files.write(tempFile, zipData);
//
//            try (ZipFile zipFile = new ZipFile(tempFile.toFile())) {
//                Enumeration<? extends ZipEntry> entries = zipFile.entries();
//
//                while (entries.hasMoreElements()) {
//                    ZipEntry entry = entries.nextElement();
//                    if (entry.isDirectory()) continue;
//
//                    try (InputStream entryStream = zipFile.getInputStream(entry)) {
//                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                        FileCopyUtils.copy(entryStream, baos);
//
//                        MultipartFile multipartFile = new MockMultipartFile(
//                                entry.getName(),
//                                entry.getName(),
//                                "application/octet-stream",
//                                baos.toByteArray()
//                        );
//                        files.add(multipartFile);
//                    }
//                }
//            }
//        } finally {
//            if (tempFile != null) {
//                try {
//                    Files.deleteIfExists(tempFile);
//                } catch (IOException e) {
//                    log.error("Failed to delete temporary file: {} 文档名称：=》》{}", e, tempFile);
//                }
//            }
//        }
//        return files;
//    }
//}
